package com.jpx.jxcbiz.web;


import com.jpx.common.dto.TCustomerDto;
import com.jpx.common.vo.ResultEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jpx.jxcbiz.entity.TCustomer;
import com.jpx.jxcbiz.service.ITCustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/biz/tCustomer")
public class TCustomerController {

    @Autowired
    ITCustomerService customerService;

    @RequestMapping("/list")
    public ResultEntity list(TCustomerDto customerDto){
        PageHelper.startPage(customerDto.getPageNo(),customerDto.getPageSize());
        TCustomer customer = new TCustomer();
        EntityWrapper<TCustomer> entityWrapper = new EntityWrapper<>(customer);
        if(StringUtils.isNotBlank(customerDto.getName())){
            entityWrapper.like("name",customerDto.getName());
        }
        List<TCustomer> list = customerService.selectList(entityWrapper);
        PageInfo<TCustomer> pageInfo = new PageInfo<>(list);
        return ResultEntity.ok(pageInfo);
    }

    @RequestMapping("/insert")
    public ResultEntity insert(TCustomerDto customerDto){

        TCustomer customer = new TCustomer();
        BeanUtils.copyProperties(customerDto,customer);
        boolean b = customerService.insert(customer);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/update")
    public ResultEntity update(TCustomerDto customerDto){
        TCustomer customer = new TCustomer();
        BeanUtils.copyProperties(customerDto,customer);
        boolean b = customerService.updateById(customer);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/delete")
    public ResultEntity delete(String ids){
        int i = customerService.deleteByIds(ids);
        return ResultEntity.ok(i);
    }

    /**
     * 查询所有的用户
     * @return
     *
     */
    @RequestMapping("/getCustomer")
    public ResultEntity getCustomer(){
        return ResultEntity.ok(customerService.getCustomer());
    }

}
